<?php

namespace Data\Drive;

class DataCache
{
    public static function setCache($file, $data)
    {
        $dir = dirname($file);
        is_dir($dir) || mkdir($dir, 0777, true);
        file_put_contents($file, "<?php\r\n return " . var_export($data, true) . ";");
    }

    public static function clearCache($table)
    {
        $dir = DATA_CACHE_DIR . $table;
        if (!is_dir($dir)) {
            return;
        }
        $fileNames = scandir($dir);
        foreach ($fileNames as $fileName) {
            $file = $dir . DS . $fileName;
            if (file_exists($file) && is_file($file)) {
                unlink($file);
            }
        }
    }
}
